package com.ccs.blockchain.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ccs.blockchain.eneity.ParamHeader;
import com.ccs.blockchain.eneity.ParamObj;
import com.ccs.blockchain.eneity.report.ReportParam;
import com.ccs.blockchain.eneity.report.ReportParamBody;
import com.ccs.blockchain.eneity.report.ReportRequestType;
import org.apache.http.Consts;
import org.apache.http.entity.StringEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("baidu/feed/report")
public class ReportController extends BaiduFeedController {

    @RequestMapping("/getRealTimeFeedData")
    public String getRealTimeFeedData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        ParamObj param = JSONObject.parseObject(httpServletRequest.getParameter("param"), ParamObj.class);
        if (param == null) {
            return null;
        }
        ReportParamBody reportParam = getReportParamBody(param.getStartDays(), param.getEndDays());
        param.setReportParamBody(reportParam);
        return service(param, "/v1/feed/cloud/ReportFeedService/getRealTimeFeedData");
    }

    private ReportParamBody getReportParamBody(Integer startDays, Integer endDays) {
        ReportRequestType realTimeRequestType = new ReportRequestType();
        List<String> performanceDatas = new ArrayList<>();
        performanceDatas.add("impression");
        performanceDatas.add("click");
        performanceDatas.add("cost");
        realTimeRequestType.setPerformanceData(performanceDatas);
        realTimeRequestType.setStartDate(getStartTime(startDays));
        realTimeRequestType.setEndDate(getEndTime(endDays));

        realTimeRequestType.setLevelOfDetails(7);
        realTimeRequestType.setReportType(703);
        realTimeRequestType.setStatRange(7);

        realTimeRequestType.setUnitOfTime(5);
        realTimeRequestType.setNumber(1000);
        ReportParamBody reportParam = new ReportParamBody();
        reportParam.setRealTimeRequestType(realTimeRequestType);
        return reportParam;
    }

    @Override
    protected StringEntity genHttpEntity(ParamObj paramObj) {
        StringEntity strEntity = new StringEntity(JSON.toJSONString(genReportParam(paramObj)), Consts.UTF_8);
        return strEntity;
    }

    private ReportParam genReportParam(ParamObj paramObj) {
        ParamHeader head = new ParamHeader();
        head.setOpUsername(paramObj.getOpUsername());
        head.setOpPassword(paramObj.getOpPassword());
        head.setTgSubname(paramObj.getTgSubname());
        head.setBceUser(paramObj.getBceUser());

        ReportParam param = new ReportParam();
        param.setHeader(head);
        param.setBody(paramObj.getReportParamBody());

        return param;
    }

    private Date getStartTime(Integer n) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -n);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    private Date getEndTime(Integer n) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -n);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
}
